<?php
include('../../../config.php');
include(PUNG_ROOT . '/admin/header.php');

$adminmenu = array(
CONTENT_001 => 'index.php',
CONTENT_002 => 'new.php',
CONTENT_015 => 'edit.php'
);

if(is_numeric($_GET['id']))
{
	$id = $_GET['id'];
	$edit = $db -> db_query("SELECT * FROM " . DB_PREFIX . "content WHERE id = '$id'");
	$edit = $db -> db_result();
	$edit = $edit['0'];
}

if(isset($_POST['submit']))
{
	$db -> db_query("UPDATE " . DB_PREFIX . "content SET 
	title = '" . addslashes($_POST['title']) . "',
	keywords = '" . addslashes($_POST['keywords']) . "',
	description = '" . addslashes($_POST['description']) . "',
	date = '" . date('Y-m-d') . "',
	content = '" . addslashes($_POST['contentfield']) . "' WHERE id = '$id'");

	redirect_with_message('msg', 'CONTENT_025', 'index.php');
}

include(PUNG_ROOT . '/admin/head.php');
?>

<!-- Tiny MCE editor -->
<script type="text/javascript" src="<?php print PUNG_URL; ?>/system/editors/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
	theme : "advanced",
	mode : "exact",
	relative_urls : false,
	elements : "contentfield",
	save_callback : "customSave",
	content_css : "example_advanced.css",
	extended_valid_elements : "a[href|target|name]",
	plugins : "table",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	//invalid_elements : "a",
	theme_advanced_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1", // Theme specific setting CSS classes
	//execcommand_callback : "myCustomExecCommandHandler",
	font_size_style_values : "xx-small,x-small,small,medium,large,x-large,xx-large",
	debug : false
});

// Custom event handler
function myCustomExecCommandHandler(editor_id, elm, command, user_interface, value) {
	var linkElm, imageElm, inst;

	switch (command) {
		case "mceLink":
			inst = tinyMCE.getInstanceById(editor_id);
			linkElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "a");

			if (linkElm)
				alert("Link dialog has been overriden. Found link href: " + tinyMCE.getAttrib(linkElm, "href"));
			else
				alert("Link dialog has been overriden.");

			return true;

		case "mceImage":
			inst = tinyMCE.getInstanceById(editor_id);
			imageElm = tinyMCE.getParentElement(inst.selection.getFocusElement(), "img");

			if (imageElm)
				alert("Image dialog has been overriden. Found image src: " + tinyMCE.getAttrib(imageElm, "src"));
			else
				alert("Image dialog has been overriden.");

			return true;
	}

	return false; // Pass to next handler in chain
}
function customSave(id, content) {
}
</script>
<!-- / Tiny MCE editor -->

<h3><?php print CONTENT_024; ?></h3>

<form action="" method="post" name="form1">
<table width="100%">
<tr>
	<td width="120"><strong><?php print CONTENT_017; ?>:</strong></td>
	<td><input type="text" style="width: 550px;" name="title" value="<?php print stripslashes($edit['title']); ?>" /></td>
</tr>
<tr>
	<td valign="top"><strong><?php print CONTENT_020; ?>:</strong></td>
	<td><textarea style="width: 530px; height: 500px" name="contentfield"><?php print stripslashes($edit['content']); ?></textarea></td>
</tr>
<tr>
	<td valign="top"><strong><?php print CONTENT_022; ?>:</strong></td>
	<td>
	<div style="width: 550px; height:200px; overflow:auto">
	<?php
	$sql = $db -> db_query("SELECT * FROM " . DB_PREFIX . "images ORDER BY id DESC");
	$sql = $db -> db_result();
	
	foreach($sql as $images)
	{
		print '<div style="float: left; width: 160px; padding: 5px;">';
		print '<img src="' . PUNG_URL . '/data/thumbs/' . $images['filename'] . '" alt="" style="cursor: pointer"';
		$insert = htmlentities('<a href="' . PUNG_URL . '/data/' . $images['filename'] . '" target="_blank"><img src="' . PUNG_URL . '/data/thumbs/' . $images['filename'] . '" alt="" /></a>');
		?>
		onclick="tinyMCE.execCommand('mceInsertContent', false, '<?php print $insert; ?>');"
		<?php
		print ' />';
		print '</div>';
	}
	?>
	<div style="clear:both;"></div>
	</div>
	</td>
</tr>
<tr>
	<td width="120"><strong><?php print CONTENT_018; ?>:</strong></td>
	<td><input type="text" style="width: 550px;" name="keywords" value="<?php print stripslashes($edit['keywords']); ?>" /></td>
</tr>
<tr>
	<td width="120"><strong><?php print CONTENT_019; ?>:</strong></td>
	<td><input type="text" style="width: 550px;" name="description" value="<?php print stripslashes($edit['description']); ?>" /></td>
</tr>
</table>
<br />
<input type="submit" name="submit" class="form_button" value="<?php print CONTENT_007; ?>" />
</form>

<?php
include(PUNG_ROOT . '/admin/footer.php');
?>